package com.wawei.seeds.me;

import android.os.Bundle;
import android.os.Message;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.wawei.seeds.R;
import com.wawei.seeds.services.WalnutPortraitServiceHandler;
import com.wawei.seeds.utils.PrefUtils;
import com.wawei.seeds.utils.RetrofitNetworkService;
import com.wawei.seeds.utils.WalnutBaseNofityHandler;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 *
 * Created by t27891 on 15/7/15.
 */
public class MeSettingsChangePwdActivity extends ActionBarActivity {
    @InjectView(R.id.moreBkg)View moreBkg;
    @InjectView(R.id.walnut_label_old_pwd)EditText oldPwd;
    @InjectView(R.id.walnut_new_password1)EditText newPwd1;
    @InjectView(R.id.walnut_new_password2)EditText newPwd2;
    @InjectView(R.id.walnut_change_pwd_submit)View submit;

    @InjectView(R.id.submittingProgress)ProgressBar submittingProgress;

    private static final int CHANGE_PASSWORD = 0x0002;
    private WalnutPortraitServiceHandler serviceHandler;
    private ChangePasswordNotifyHandler notifyHandler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_password);
        ButterKnife.inject(this);

        serviceHandler = new WalnutPortraitServiceHandler(this);
        notifyHandler = new ChangePasswordNotifyHandler(this);

        moreBkg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (v.getId() == R.id.moreBkg) {
                    MeSettingsChangePwdActivity.this.finish();
                }
            }
        });

        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                submit();
            }
        });
    }

    private void submit(){
        String oldPwd = this.oldPwd.getText().toString();
        String newPwd1 = this.newPwd1.getText().toString();
        String newPwd2 = this.newPwd2.getText().toString();

        if(null == oldPwd || oldPwd.trim().length() == 0){
            Toast.makeText(this, R.string.walnut_input_old_password, Toast.LENGTH_SHORT).show();
            this.oldPwd.setFocusable(true);
            return;
        }
        if(null == newPwd1 || newPwd1.trim().length() == 0){
            Toast.makeText(this, R.string.walnut_input_new_password, Toast.LENGTH_SHORT).show();
            this.newPwd1.setFocusable(true);
            return;
        }
        if(null == newPwd2 || newPwd2.trim().length() == 0){
            Toast.makeText(this, R.string.walnut_input_confirm_password, Toast.LENGTH_SHORT).show();
            this.newPwd2.setFocusable(true);
            return;
        }
        if(!newPwd1.equals(newPwd2)){
            Toast.makeText(this, R.string.walnut_new_password_nomatch, Toast.LENGTH_SHORT).show();
            this.newPwd1.setFocusable(true);
            return;
        }
        Bundle bundle = new Bundle();
        bundle.putString(WalnutPortraitServiceHandler.OLD_PASSWORD, oldPwd);
        bundle.putString(WalnutPortraitServiceHandler.NEW_PASSWORD, newPwd1);

        serviceHandler.changeMyPassword(RetrofitNetworkService.makeAsyncRestAdapter(), bundle, notifyHandler, CHANGE_PASSWORD);
        submittingProgress.setVisibility(View.VISIBLE);
    }

    private static class ChangePasswordNotifyHandler extends WalnutBaseNofityHandler<MeSettingsChangePwdActivity, Void>{
        public ChangePasswordNotifyHandler(MeSettingsChangePwdActivity meSettingsChangePwdActivity) {
            super(meSettingsChangePwdActivity);
        }

        @Override
        public void handleOtherMessage(Message msg) {
            switch (msg.what){
                case CHANGE_PASSWORD:
                    get().submittingProgress.setVisibility(View.GONE);
                    Bundle bundle = msg.getData();
                    if(!bundle.getString(RetrofitNetworkService.RESULT_ERROR).equals(RetrofitNetworkService.RESULT_ERROR_NONE)){
                        Toast.makeText(get(), R.string.walnut_change_password_error, Toast.LENGTH_SHORT).show();
                        return;
                    }
                    PrefUtils.markUserPassword(get(), PrefUtils.getPrefUser(get()), get().newPwd1.getText().toString());
                    get().finish();
                    break;
            }
        }
    }
}
